import axios from "axios";
axios.defaults.timeout = 2000;//请求超时时间
axios.defaults.baseURL = 'http://ceshi5.dishait.cn';
import store from '@/store/index'
// console.log(store.state.token);

//添加请求拦截器
axios.interceptors.request.use((config) => {
    //添加headers头的token
    let token = store.state.token

    if (token) {
        config.headers.token = token
    }
    return config
}, (error) => {
    return error
})

export function request(methods, url, data) {
    var p = new Promise((resolve, reject) => {
        if (methods == 'post') {
            axios.post(url, data).then(res => {
                resolve(res)
            }).catch(error => {
                reject(error)
            })
        } else if (methods == 'get') {
            axios.get(url).then(res => {
                resolve(res)
            }).catch(error => {
                reject(error)
            })
        }
    })
    return p
}